package com.jctrip.hotel.management.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jctrip.hotel.common.util.BeanUtil;
import com.jctrip.hotel.common.util.JsonUtil;
import com.jctrip.hotel.domain.dao.HtHotelStdInfoDao;
import com.jctrip.hotel.domain.dao.HtRouteInfoDao;
import com.jctrip.hotel.domain.dao.HtSourceCodeDao;
import com.jctrip.hotel.domain.entity.HtHotelStdInfo;
import com.jctrip.hotel.domain.entity.HtRouteInfo;
import com.jctrip.hotel.domain.entity.HtSourceCode;
import com.jctrip.hotel.management.client.FeizhuClient;
import com.jctrip.hotel.management.model.Extra;
import com.jctrip.hotel.management.model.param.*;
import com.jctrip.hotel.management.model.result.GetFliggyHotelResult;
import com.jctrip.hotel.management.model.result.GetHotelInfoResult;
import com.jctrip.hotel.management.model.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class HotelInfoService {
    @Autowired
    private HtHotelStdInfoDao htHotelStdInfoDao;

    @Autowired
    private HtSourceCodeDao htSourceCodeDao;

    @Autowired
    private HtRouteInfoDao htRouteInfoDao;

    @Autowired
    private FeizhuClient feizhuClient;

    public GetHotelInfoResult gethotelInfo(GetHotelInfoParam param){
        if(StringUtils.isBlank(param.getSourceCode())){
            throw new RuntimeException("供应渠道不能为空");
        }
        LambdaQueryWrapper<HtHotelStdInfo> htHotelStdInfoQueryWrapper = new LambdaQueryWrapper<>();
        htHotelStdInfoQueryWrapper.eq(HtHotelStdInfo::getSourceCode, param.getSourceCode());
        if(StringUtils.isNotBlank(param.getHotelName())){
            htHotelStdInfoQueryWrapper.like(HtHotelStdInfo::getHotelName, param.getHotelName());
        }
        Page<HtHotelStdInfo> page = new Page<>();
        page.setSize(param.getPageSize());
        page.setCurrent(param.getPageNo());
        Page<HtHotelStdInfo> htHotelStdInfoPage = htHotelStdInfoDao.selectPage(page, htHotelStdInfoQueryWrapper);
        List<GetHotelInfoResult.HotelInfo> hotelInfoList = new ArrayList<>();

        htHotelStdInfoPage.getRecords().forEach(htHotelStdInfo -> {
            GetHotelInfoResult.HotelInfo hotelInfo = GetHotelInfoResult.HotelInfo.builder().build();
            BeanUtil.copyProperties(htHotelStdInfo, hotelInfo);
            HtSourceCode htSourceCode = htSourceCodeDao.selectOne(new LambdaQueryWrapper<HtSourceCode>()
                    .eq(HtSourceCode::getCode, htHotelStdInfo.getSourceCode()));
            if(null != htSourceCode) {
                hotelInfo.setSourceName(htSourceCode.getName());
            }
            hotelInfoList.add(hotelInfo);
        });

        return GetHotelInfoResult.builder()
                .hotelInfoList(hotelInfoList)
                .pageNo(htHotelStdInfoPage.getCurrent())
                .pageSize(htHotelStdInfoPage.getSize())
                .total(htHotelStdInfoPage.getTotal())
                .build();
    }

    public Boolean editHotelsStatus(UpdateHotelsStatusParam param){
        if(param.getBookStatus() == null){
            throw new RuntimeException("状态不能为空");
        }
        if(CollectionUtils.isEmpty(param.getHotelIds())){
            throw new RuntimeException("修改酒店列表不能为空");
        }

        param.getHotelIds().forEach(hotelId ->{
            htHotelStdInfoDao.update(null, new LambdaUpdateWrapper<HtHotelStdInfo>()
                    .set(HtHotelStdInfo::getBookStatus, param.getBookStatus())
                    .eq(HtHotelStdInfo::getHotelId, hotelId));
        });

        return true;
    }

    public Result<GetFliggyHotelResult> getFliggyHotels(GetFliggyHotelParam param){
        if(StringUtils.isBlank(param.getRouteId())){
            throw new RuntimeException("渠道账户不能为空");
        }
        HtRouteInfo htRouteInfo = Optional.ofNullable(htRouteInfoDao.selectOne(new LambdaUpdateWrapper<HtRouteInfo>()
                .eq(HtRouteInfo::getRouteId, param.getRouteId()))).orElse(new HtRouteInfo());
        if(null == htRouteInfo.getRouteId()){
            throw new RuntimeException("参数错误");
        }
        param.setPageNo(param.getPageNo() != null && param.getPageNo() > 0L ? param.getPageNo() : 1L);
        param.setPageSize(param.getPageSize() != null && param.getPageSize() > 0L? param.getPageSize() : 10L);
        param.setRouteType(htRouteInfo.getRouteType());
        return feizhuClient.getFliggyHotels(param);
    }

    public Result<Boolean> updateFliggyHotelsStatus(UpdateFliggyHotelsStatusParam param){
        if(StringUtils.isBlank(param.getRouteId())){
            throw new RuntimeException("渠道账户不能为空");
        }
        if(CollectionUtils.isEmpty(param.getHids())){
            throw new RuntimeException("酒店不能为空");
        }
        if(null == param.getStatus()){
            throw new RuntimeException("状态不能为空");
        }
        HtRouteInfo htRouteInfo = Optional.ofNullable(htRouteInfoDao.selectOne(new LambdaUpdateWrapper<HtRouteInfo>()
                .eq(HtRouteInfo::getRouteId, param.getRouteId()))).orElse(new HtRouteInfo());
        if(null == htRouteInfo.getExtra()){
            throw new RuntimeException("参数错误");
        }
        param.setAppId(htRouteInfo.getThirdAppId());
        param.setSecret(htRouteInfo.getThirdSecret());
        param.setSession(JsonUtil.toObject(Extra.class, htRouteInfo.getExtra()).getSession());
        param.setRouteType(htRouteInfo.getRouteType());
        param.setVendor(JsonUtil.toObject(Extra.class, htRouteInfo.getExtra()).getVendor());
        return feizhuClient.updateFliggyHotelsStatus(param);
    }


    public Result<Boolean> updateFliggyHotelsPrice(UpdateFliggyHotelsPriceParam param){
        if(StringUtils.isBlank(param.getRouteId())){
            throw new RuntimeException("渠道账户不能为空");
        }
        HtRouteInfo htRouteInfo = Optional.ofNullable(htRouteInfoDao.selectOne(new LambdaUpdateWrapper<HtRouteInfo>()
                .eq(HtRouteInfo::getRouteId, param.getRouteId()))).orElse(new HtRouteInfo());
        if(null == htRouteInfo.getExtra()){
            throw new RuntimeException("参数错误");
        }
        param.setRouteType(htRouteInfo.getRouteType());
        return feizhuClient.updateFliggyHotelsPrice(param);
    }

    public Result<Boolean> matchHotelSelf(UpdateHotelMatchParam param){
        if(StringUtils.isBlank(param.getRouteId())){
            throw new RuntimeException("渠道账户不能为空");
        }
        HtRouteInfo htRouteInfo = Optional.ofNullable(htRouteInfoDao.selectOne(new LambdaUpdateWrapper<HtRouteInfo>()
                .eq(HtRouteInfo::getRouteId, param.getRouteId()))).orElse(new HtRouteInfo());
        if(null == htRouteInfo.getExtra()){
            throw new RuntimeException("参数错误");
        }
        param.setAppId(htRouteInfo.getThirdAppId());
        param.setSecret(htRouteInfo.getThirdSecret());
        param.setSession(JsonUtil.toObject(Extra.class, htRouteInfo.getExtra()).getSession());
        param.setRouteType(htRouteInfo.getRouteType());
        return feizhuClient.matchHotelSelf(param);
    }
}
